<template>
	<view>
		<head-nav text-content="老年大学" :is-back-show="false"></head-nav>
		<view class="main_box">
			<notice-bar :notices="noticeList" v-if="isShow1"></notice-bar>
			<view class="tags flex">
				<view class="tag_item flex alignC">
					<span class="dot" style="background: #FA3534;"></span>社区活动
				</view>
				<view class="tag_item flex alignC" style="margin-left: 20rpx;">
					<span class="dot" style="background: #FF9900;"></span>课程信息
				</view>
			</view>
			<view class="calendar_box">
				<uni-calendar :insert="true" :lunar="true" @change="changeDate" :selected="selected" :showMonth="true"
					@monthSwitch="change" />
			</view>
			<!-- 老年资讯 -->
			<view class="information_box">
				<view class="title flex fjb alignC" @click="toPage('/subPackages/elderlyUniversityList/elderlyUniversityList')">
					<view>老年资讯</view>
					<u-icon name="arrow-right" color="#303133" size="24"></u-icon>
				</view>
				<view class="list_box" v-if="list.length">
					<view class="list_item flex" v-for="(item, index) in list" :key="index" @click="handleClickDetail(item)">
						<view class="left">
							<image class="list_item_img" :src="item.coverImgs[0].url" mode="aspectFill"></image>
						</view>
						<view class="right flex flexColumn">
							<view class="item_title line2">
								{{ item.title }}
							</view>
							<view class="bottom flex">
								<view class="item_date">
									{{ item.updateTime | getTimeToMin }}
								</view>
								<view class="list_item_like" @click.stop="toggleLike(item.like,index)">
									<image v-if="item.like" src="/static/like_active.png" mode=""></image>
									<image v-else src="/static/like.png" mode=""></image>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="empty_box" v-else style="height: 30vh;">
					<image src="/static/empty1.png" mode=""></image>
					<text>暂无资讯</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import headNav from '@/components/HeadNav.vue';
	import {
		noticeBar
	} from '@/components/notice.vue'
	import {
		mapState
	} from 'vuex'
	import {
		queryListByPageApi,
		queryCategoryListApi
	} from "@/common/API/articleApi.js"
	export default {
		components: {
			headNav,
			noticeBar
		},
		data() {
			return {
				noticeList: [],
				show: true,
				mode: 'range',
				dateList: [
					{time: '2024-07-07', type: [1, 2]},
					{time: '2024-07-17', type: [1, 2]},
					{time: '2024-07-27', type: [1]},
					{time: '2024-07-30', type: [2]},
				],
				selected: [],
				list: [],
				pageNum: 1,
				pageSize: 10,
				total: 0,
				isShow: false,
				categoryId: '',
				classesId: '',
				isShow1: false
			}
		},
		onLoad() {
			this.selected = this.dateList.map((el) => {
				return {
					date: el.time,
					info: el.type
				}
			})
		},
		computed: {
			...mapState('user', ['ylPhone', 'ylUserId', 'ylUserInfo', 'ylToken', 'ylEnterprise', 'ylProjectCode']),
		},
		async onShow() {
			await this.getCateId('老年资讯')
			this.list = []
			this.total = 0
			this.isShow = false
			this.pageNum = 1
			this.listByPage()
			
			// this.$nextTick(() => {
				this.noticeList = []
				this.isShow1 = false
				this.listNoticeByPage()
			// });
		},
		async onReachBottom() {
			if (this.list.length >= this.total) {
				return uni.$showMsg('已经到底了')
			}
			this.pageNum = this.pageNum + 1
			await this.listByPage()
		},
		methods: {
			async listByPage() {
				let params = {
					pageNum: this.pageNum,
					pageSize: 2,
					categoryId: this.categoryId,
					classes: this.classesId,
					projectCode: this.ylProjectCode
				}
				const res = await queryListByPageApi(params)
				if (res.status == 200) {
					this.list.push(...res.data.records)
					console.log('我的收藏', this.list)
					if (this.list.length > 0) {
						this.isShow = true
					}
					this.total = res.data.total
					this.list.forEach((item) => {
						item.like = false
					})
				}
				
			},
			// 消息通知
			async listNoticeByPage() {
				let res = await queryCategoryListApi('消息通知')
				let ylProjectCode = this.ylProjectCode
				if(res.status == 200) {
					let classesId = res.data.records[0].id
					let categoryId = res.data.records[0].parentId
				
					let params = {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						categoryId: categoryId,
						classes: classesId,
						projectCode: this.ylProjectCode
					}
					const res1 = await queryListByPageApi(params)
					if (res1.status == 200) {
						this.isShow1 = true
						this.noticeList.push(...res1.data.records)
						
					}
				}
				
			},
			
			async getCateId(name) {
				let res = await queryCategoryListApi(name)
				console.log(res,'==========')
				if(res.status == 200) {
					this.classesId = res.data.records[0].id
					this.categoryId = res.data.records[0].parentId
				}
			},
			toggleLike(isLike,index) {
				this.list[index].like = !isLike
				this.$forceUpdate();
			},
			handleClickDetail(item) {
				console.log(item,'====')
				this.$emit("click", item);
				uni.navigateTo({
					url: `/subPackages/articleDetail/articleDetail?item=${encodeURIComponent(JSON.stringify(item))}&title=${'老年资讯'}`
				})
			},
			changeDate() {
				this.selected = this.dateList.map((el) => {
					return {
						date: el.time,
						info: el.type
					}
				})
			},
		}
	}
</script>

<style>
	page{
		height: 100vh;
	}
</style>
<style scoped lang="scss">
	/deep/ .uni-calendar-item--isDay, /deep/.uni-calendar-item--checked {
		background: #52C41A !important;
		color: #FFFFFF;
	}
	 .dot {
	    background-color: red;
	    color: white;
	    border-radius: 50%;
	    width: 10px;
	    height: 10px;
	  }
	.main_box {
		margin: 17rpx 32rpx 32rpx 32rpx;

		.tags {
			.dot {
				display: block;
				width: 8rpx;
				height: 8rpx;
				border-radius: 50%;
				margin-right: 8rpx;
			}
		}

		.information_box {
			.title {
				height: 106rpx;
				color: #303133;
				font-size: 30rpx;
				font-weight: 600;
			}
		}

		.list_box {
			overflow: scroll;
			height: 300rpx;
			.list_item {
				margin-bottom: 32rpx;
				.left {
					width: 220rpx;
					height: 136rpx;
					margin-right: 16rpx;

					.list_item_img {
						width: 100%;
						height: 100%;
						border-radius: 16rpx;
					}
				}

				.right {
					flex: 1;

					.item_title {
						font-size: 30rpx;
						color: #000000;
					}

					.bottom {
						flex: 1;
						align-items: flex-end;
						justify-content: space-between;
					}

					.item_date {
						font-size: 28rpx;
						color: #999999;
					}

					.list_item_like {
						width: 40rpx;
						height: 40rpx;
						image{
							width: 100%;
							height: 100%;
						}
					}
				}
			}
		}
	}
</style>